Skip to content

[Bug Fix] Accordion: hide closed content properly instead of zero height (#168)#433

Merged
cirdes merged 2 commits into
mainfrom
fix/accordion-visibility
Jun 22, 2026
Merged

[Bug Fix] Accordion: hide closed content properly instead of zero height (#168)#433
cirdes merged 2 commits into
mainfrom
fix/accordion-visibility

Conversation

@djalmaaraujo

@djalmaaraujo djalmaaraujo commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Closes #168

How to reproduce

  1. Place a FormField with a FormFieldError inside a closed AccordionItem.
  2. Submit the form without filling the field.
  3. Observe the validation error is invisible — the content is clipped to height: 0 with overflow: hidden, so the error message can't be seen and the browser cannot scroll to / focus the invalid field.

What changed

Before: AccordionContent started with style="height: 0px;" and overflow-y-hidden. The Stimulus controller only animated height between 0 and scrollHeight. The element was always in the layout (and form-focus) tree, just visually hidden.

After:

  • AccordionContent now renders with hidden attribute and data-state="closed" by default, completely removing it from layout and form-focus.
  • The Stimulus controller (accordion_controller.js) now:
    • On open: removes hidden and sets data-state="open" before animating height, so scrollHeight is measurable.
    • On close: animates height to 0, then calls .finished.then(...) to set hidden after the animation completes (guarded by data-state === "closed" to avoid racing a rapid re-open).
  • Animation remains smooth — the motion library and existing duration/easing values are unchanged.
  • Public Ruby API is identical: AccordionContent, AccordionItem, AccordionTrigger, targets, and values are all preserved.

Testing instructions

Automated tests

cd gem
bundle exec rake test TEST=test/ruby_ui/accordion_test.rb  # accordion-specific
bundle exec rake                                            # full suite + lint

Manual browser verification

  1. Open an accordion page (e.g. https://rubyui.com/docs/accordion or your local dev server).
  2. Open DevTools → Elements. Inspect a closed AccordionContent — verify it has hidden attribute and data-state="closed".
  3. Click the trigger to open it — verify hidden is removed, data-state becomes "open", and the animation is smooth.
  4. Close it again — verify the height animates to 0, then hidden reappears on the element.
  5. Form error scenario: wrap an <input required> and an error <span> inside a closed AccordionContent, submit the form empty, then open the accordion — confirm the validation error is visible and the field is focusable.

Summary by cubic

Fixes the Accordion so closed content is truly removed from layout and focus using the hidden attribute instead of height: 0 clipping. Prevents invisible validation errors while keeping open/close animations smooth.

  • Bug Fixes
    • AccordionContent now renders with hidden and data-state="closed".
    • On open: remove hidden, set data-state="open", then animate to measured height.
    • On close: animate to height: 0, then set hidden; guarded by data-state to avoid races.
    • Tests clarified and renamed with an extra visible-content assertion; rebuilt mcp/data/registry.json for CI; public API unchanged.

Written for commit 837712f. Summary will update on new commits.

Review in cubic

…ght (#168)

Closed accordion content was rendered with height:0 + overflow-hidden,
which trapped form field errors in an invisible, still-focusable region.
Now the content element receives the `hidden` attribute after the close
animation completes (and `data-state="closed"`), fully removing it from
layout and form focus. On open, `hidden` is removed before the height
animation begins. Animation remains smooth via motion.
@djalmaaraujo djalmaaraujo requested a review from cirdes as a code owner June 22, 2026 20:08

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread gem/test/ruby_ui/accordion_test.rb Outdated
Rename `test_open_content_does_not_have_hidden` to `test_open_item_wires_stimulus_open_value`,
add a `Visible content` assertion, and clarify comments to accurately describe
that the hidden attribute is removed at JS runtime (not server-render time).
Addresses code-review finding from cubic (confidence 9).

Also rebuild mcp/data/registry.json to include the accordion_content.rb and
accordion_controller.js changes from this PR so CI "MCP registry up to date" passes.
@djalmaaraujo

Copy link
Copy Markdown
Contributor Author

Addressed the code-review finding from cubic (confidence 9):

  • Renamed test_open_content_does_not_have_hiddentest_open_item_wires_stimulus_open_value. The old name was misleading because at the server-rendered HTML level the content always starts with hidden — it is the Stimulus controller that removes hidden and sets data-state="open" at runtime on connect. The test now accurately names what it asserts (that open: true wires up the correct Stimulus data attribute) and adds an assertion confirming the content text is present in the DOM.

  • Rebuilt mcp/data/registry.json via bundle exec exe/ruby-ui-mcp-build to include the accordion component changes, fixing the "MCP registry up to date" CI check.

@cirdes cirdes merged commit 0897b2a into main Jun 22, 2026
8 checks passed
@cirdes cirdes deleted the fix/accordion-visibility branch June 22, 2026 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accordion is using height to manage visibility

2 participants